/*
 * @Descripttion:自定义指令抽离
 * @version:
 * @Author: zhangfan
 * @email: 2207044692@qq.com
 * @Date: 2020-08-28 16:20:15
 * @LastEditors: zhangfan
 * @LastEditTime: 2020-08-28 16:37:39
 */
import store from '../store'

/**
 * @description: 自定义指令 防止多次点击
 */
const sdc = {
  inserted(el, binding) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, 2000)
      }
    })
  },
}

// 是否有权限
const hasPermission = (userPermission) => {
  let userPermissionList = Array.isArray(userPermission)
    ? userPermission
    : [userPermission]
  // 当前用户的权限列表
  let permissionList = store.state.buttonLimitsArr
  return userPermissionList.some((e) => permissionList.includes(e))
}

/**
 * @description: 权限指令，单个权限传字符，多个传数组
 */
const hasBtn = {
  bind: (el, binding, vnode) => {
    if (!hasPermission(binding.value)) {
      el.parentNode.removeChild(el)
    }
  },
}
export default {
  sdc,
  hasBtn,
}
